package com.uchiha.nova.mainService.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.uchiha.nova.core.model.dto.OilUserDTO;
import cn.hutool.core.bean.BeanUtil;
import com.uchiha.nova.core.tool.constant.NovaCRUDMethodConstant;
import com.uchiha.nova.core.model.entity.OilUserEntity;
import com.uchiha.nova.core.model.vo.OilUserVO;
import com.uchiha.nova.core.model.query.OilUserQuery;
import com.uchiha.nova.core.tool.api.R;
import com.uchiha.nova.core.tool.support.BaseController;
import com.uchiha.nova.core.tool.support.Condition;
import com.uchiha.nova.core.tool.support.QueryContext;
import com.uchiha.nova.core.tool.util.Func;
import com.uchiha.nova.mainService.service.IOilUserService;
import com.uchiha.nova.mainService.wrapper.OilUserWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;


/**
 * 业务人员账号 控制器
 *
 * @author Uchihashiamu
 */
@RestController
@AllArgsConstructor
@RequestMapping("/oilUser")
@Tag(name = "业务人员账号")
@Slf4j
public class OilUserController extends BaseController {

    @Resource
    IOilUserService oilUserService;

    /**
     * 详情
     */
    @PostMapping(NovaCRUDMethodConstant.NOVA_WEB_GETONE)
    @Operation(summary = "详情")
    public R<OilUserVO> getOne(@RequestBody OilUserDTO oilUserDTO) {
        OilUserEntity entity = new OilUserEntity();
        BeanUtil.copyProperties(oilUserDTO, entity);
        OilUserEntity detail = oilUserService.getOne(Condition.getQueryWrapper(entity));
        return R.data(OilUserWrapper.build().entityVO(detail));
    }

    /**
     * 分页 代码自定义代号
     */
    @GetMapping(NovaCRUDMethodConstant.NOVA_WEB_PAGE)
    @Operation(summary = "分页查询")
    public R getPage(OilUserQuery query, QueryContext queryContext) {
        IPage<OilUserEntity> pages = oilUserService.page(Condition.getPage(queryContext), Condition.getQueryWrapper(query, queryContext, OilUserEntity.class));
        return R.data(OilUserWrapper.build().pageVO(pages));
    }

    /**
     * 新增 代码自定义代号
     */
    @PostMapping(NovaCRUDMethodConstant.NOVA_WEB_SAVE)
    @Operation(summary = "新增")
    public R save(@RequestBody OilUserDTO oilUserDTO) {
        Long a = oilUserService.count(new QueryWrapper<OilUserEntity>().eq("username", oilUserDTO.getUsername()));
        if (a.compareTo(0L) == 1) {
            return R.fail("重复的用户名");
        }

        return oilUserService.saveOilUser(oilUserDTO);
    }

    /**
     * 修改 代码自定义代号
     */
    @PostMapping(NovaCRUDMethodConstant.NOVA_WEB_UPDATE)
    @Operation(summary = "修改")
    public R update(@RequestBody OilUserDTO oilUserDTO) {

        return oilUserService.updateOilUser(oilUserDTO);
    }

    /**
     * 新增或修改 代码自定义代号
     */
    @PostMapping(NovaCRUDMethodConstant.NOVA_WEB_SAVEORUPDATE)
    @Operation(summary = "新增或修改")
    public R saveOrUpdate(@RequestBody OilUserDTO oilUserDTO) {
        OilUserEntity entity = new OilUserEntity();
        BeanUtil.copyProperties(oilUserDTO, entity);
        return R.status(oilUserService.saveOrUpdate(entity));
    }


    /**
     * 删除 代码自定义代号
     */
    @PostMapping(NovaCRUDMethodConstant.NOVA_WEB_REMOVE)
    @Operation(summary = "逻辑删除")
    public R remove(@RequestParam String ids) {
        return R.status(oilUserService.deleteLogic(Func.toLongList(ids)));
    }

    /**
     * 物理删除，不可恢复
     */
    @Operation(summary = "物理删除")
    @DeleteMapping(NovaCRUDMethodConstant.NOVA_WEB_DELETE)
    public R delete(@RequestParam String ids) {
        return R.status(oilUserService.deletePhysics(Func.toLongList(ids)));
    }
}
